package com.zh.api.controller.family;

import com.alibaba.fastjson.JSONObject;
import com.zh.common.annotation.Log;
import com.zh.common.base.BaseController;
import com.zh.common.utils.*;
import com.zh.familyInfo.domain.IllChronicDO;
import com.zh.familyInfo.service.IllChronicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@Api("老人疾病信息【控制层慢病病史】")
public class IllChronicController extends BaseController {
	@Autowired
	private IllChronicService illChronicService;

	@Log("获取慢病病史列表数据")
	@ResponseBody
	@RequestMapping(value = "listIllChronic",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO listIllChronic(@RequestBody Map<String, Object> params){
		if (CheckEmptyUtil.isEmpty(params)) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		params.put("familyNumber",getFamilyUser().getNumber());
		//查询列表数据
        Query query = new Query(params);
		List<IllChronicDO> illChronicList = illChronicService.list(query);
		int total = illChronicService.count(query);
		PageUtils pageUtils = new PageUtils(illChronicList, total);
		return ResultVO.success(pageUtils);
	}


	@Log("获取慢病病史个人数据")
	@ResponseBody
	@RequestMapping(value = "editIllChronicById",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO editIllChronicById(@RequestBody IllChronicDO illChronic){
		if (illChronic==null|| CheckEmptyUtil.isEmpty(illChronic.getId())) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		IllChronicDO illChronicDO = illChronicService.get(illChronic.getId());
		return ResultVO.success(illChronicDO);
	}

	@Log("保存慢病病史信息")
	@ResponseBody
	@RequestMapping(value = "saveIllChronic",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO saveIllChronic(@RequestBody IllChronicDO illChronic){
		if (illChronic==null) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		illChronic.setFamilyNumber(getFamilyUser().getNumber());
		if(illChronicService.save(illChronic)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}

	@Log("更新慢病病史信息")
	@ResponseBody
	@RequestMapping(value = "updateIllChronic",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO updateIllChronic(@RequestBody IllChronicDO illChronic){
		if (illChronic==null) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		illChronicService.update(illChronic);
		return ResultVO.success();
	}

	@Log("根据标识id删除慢病病史信息")
	@ResponseBody
	@RequestMapping(value = "removeIllChronicById",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO removeIllChronicById(@RequestBody IllChronicDO illChronic){
		if (illChronic==null|| CheckEmptyUtil.isEmpty(illChronic.getId())) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(illChronicService.remove(illChronic.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}
    @RequestMapping(value = "batchRemoveIllChronicByIds", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation("批量删除慢病病史信息")
    public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
		if(jsonParam==null){
	    return ResultVO.error(ResultEnum.PARAME_ERROR);
	    }
	    List<String> ids= (List<String>)jsonParam.get("ids");
    if(CheckEmptyUtil.isEmpty(ids)){
	    return ResultVO.error(ResultEnum.PARAME_ERROR);
	    }
	    String[] strs1=ids.toArray(new String[ids.size()]);
    if(illChronicService.batchRemove(strs1)>0){
	    return ResultVO.success();
	    }
	    return ResultVO.error();
}
}
